//
//  AvtivityDetailPageVC.m
//  NewNest
//
//  Created by hesiyao on 16/1/19.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "AvtivityDetailPageVC.h"
#import "UIView+height.h"
#import <UIImageView+WebCache.h>
@interface AvtivityDetailPageVC () <UIWebViewDelegate>
@property (nonatomic, retain) UIImageView *backImg;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UIImageView *contentImg;/**< 放内容的视图 */
@property (nonatomic, retain) UIWebView *contentView;/**< 内容的webview */
@property (nonatomic, retain) UILabel *dateLabel;
@property (nonatomic ,retain) UILabel *addressLabel;
@property (nonatomic, retain) UIScrollView *scrollview;/**< 滚动视图 */
@end

@implementation AvtivityDetailPageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // navigation设置
    self.navigationItem.title = @"活动详情";
    
    //视图创建
    [self createSubviews];
    
//    [self addObserverForWebViewContentSize];
    
}

//- (void)webViewDidFinishLoad:(UIWebView *)webView
//{
//    // 获取webview的高度
//    NSString *htmlHeight = [_contentView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"];
//    CGFloat realHeight = [htmlHeight floatValue];
//    
//    _contentView.frame = CGRectMake(0, 0, self.view.frame.size.width, realHeight);
//    
//    NSLog(@"%lf", realHeight);
//    
//    // 日期
//    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _contentView.frame.size.height, self.view.frame.size.width - 20, self.view.frame.size.height / 20)];
//    _dateLabel.font = [UIFont systemFontOfSize:15];
//    _dateLabel.backgroundColor = [UIColor greenColor];
//    _dateLabel.textColor = [UIColor grayColor];
//    [_contentView.scrollView addSubview:_dateLabel];
//    [_dateLabel release];
//    //地址
//    self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _dateLabel.frame.origin.y + _dateLabel.frame.size.height + 5 , self.view.frame.size.width - 20, _dateLabel.frame.size.height)];
//    _addressLabel.font = [UIFont systemFontOfSize:15];
//    _addressLabel.backgroundColor = [UIColor orangeColor];
//    _addressLabel.textColor = [UIColor grayColor];
//    [_contentView.scrollView addSubview:_addressLabel];
//    [_addressLabel release];
//
//}

/// 视图创建
- (void)createSubviews
{
//    // 滚动视图
//    self.scrollview = [[UIScrollView alloc] initWithFrame:self.view.bounds];
//    [self.view addSubview:_scrollview];
//    [_scrollview release];
    
    // 内容的webview
    self.contentView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [_contentView loadHTMLString:_modal.content baseURL:nil];
    
    
    
//    //获取WebView最佳尺寸（点）
//    CGSize frame = [_contentView sizeThatFits:_contentView.frame.size];
//    //获取内容实际高度（像素）
//    NSString * height_str= [_contentView stringByEvaluatingJavaScriptFromString: @"document.getElementById('webview_content_wrapper').offsetHeight + parseInt(window.getComputedStyle(document.getElementsByTagName('body')[0]).getPropertyValue('margin-top'))  + parseInt(window.getComputedStyle(document.getElementsByTagName('body')[0]).getPropertyValue('margin-bottom'))"];
//    float height = [height_str floatValue];
//    //内容实际高度（像素）* 点和像素的比
//    height = height * frame.height / realHeight;
//    //再次设置WebView高度（点）
//    _contentView.frame = CGRectMake(0, 0, self.view.frame.size.width, height);
    
    // 设置代理人
    _contentView.delegate = self;
    _contentView.scrollView.contentInset = UIEdgeInsetsMake(self.view.frame.size.height / 2, 0, 0, 0);
    [self.view addSubview:_contentView];
    [_contentView release];
    
    // 用于放置webview上方所有视图
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, - self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2)];
    [self.contentView.scrollView addSubview:view];
    view.backgroundColor = [UIColor whiteColor];
    [view release];
    
    // 上方背景图
    self.backImg = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, view.frame.size.width - 20, view.frame.size.height / 2)];
    _backImg.layer.cornerRadius = 5;
    _backImg.clipsToBounds = YES;
    [view addSubview:_backImg];
    [_backImg release];
    // 标题
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,_backImg.frame.origin.y + _backImg.frame.size.height + 10, view.frame.size.width - 20, _backImg.frame.size.height / 2)];
    _titleLabel.numberOfLines = 0;
    _titleLabel.font = [UIFont systemFontOfSize:19];
    [view addSubview:_titleLabel];
    [_titleLabel release];
    
    // 日期
    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _titleLabel.frame.origin.y + _titleLabel.frame.size.height, self.view.frame.size.width - 20, (view.frame.size.height / 2 - _titleLabel.frame.size.height - 25) / 2)];
    _dateLabel.font = [UIFont systemFontOfSize:15];
//    _dateLabel.backgroundColor = [UIColor greenColor];
    _dateLabel.textColor = [UIColor grayColor];
    [view addSubview:_dateLabel];
    [_dateLabel release];
    //地址
    self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _dateLabel.frame.origin.y + _dateLabel.frame.size.height + 5 , self.view.frame.size.width - 20, _dateLabel.frame.size.height)];
    _addressLabel.font = [UIFont systemFontOfSize:15];
//    _addressLabel.backgroundColor = [UIColor orangeColor];
    _addressLabel.textColor = [UIColor grayColor];
    [view addSubview:_addressLabel];
    [_addressLabel release];
    

    
    //给各个label赋值
    [_backImg sd_setImageWithURL:[NSURL URLWithString:_modal.background]];
    _titleLabel.text = _modal.name;
    //    _contentLabel.text = _modal.content;
    _dateLabel.text = _modal.time;
    _addressLabel.text = _modal.areaDetail;
    
    // 内容
    // 内容的高度
//    CGFloat h = [UILabel heightWith:_modal.content size:CGSizeMake(self.view.frame.size.width, 1000)];
//    self.contentImg = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10 + _titleLabel.frame.origin.y + _titleLabel.frame.size.height, self.view.frame.size.width - 20, h)];
////    _contentImg.numberOfLines = 0;
//    [self.scrollview addSubview:_contentImg];
//    [_contentImg release];
    
//    // 日期
//    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _contentView.frame.size.height, self.view.frame.size.width - 20, self.view.frame.size.height / 20)];
//    _dateLabel.font = [UIFont systemFontOfSize:15];
//    _dateLabel.backgroundColor = [UIColor greenColor];
//    _dateLabel.textColor = [UIColor grayColor];
//    [_contentView.scrollView addSubview:_dateLabel];
//    [_dateLabel release];
//    //地址
//    self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _dateLabel.frame.origin.y + _dateLabel.frame.size.height + 5 , self.view.frame.size.width - 20, _dateLabel.frame.size.height)];
//    _addressLabel.font = [UIFont systemFontOfSize:15];
//    _addressLabel.backgroundColor = [UIColor orangeColor];
//    _addressLabel.textColor = [UIColor grayColor];
//    [_contentView.scrollView addSubview:_addressLabel];
//    [_addressLabel release];
    
    
//     给ScrollView设置contentinset
//    _scrollview.contentSize = CGSizeMake(self.view.frame.size.width, _backImg.frame.size.height + _titleLabel.frame.size.height + _contentImg.frame.size.height + _dateLabel.frame.size.height + _addressLabel.frame.size.height + 55) ;
    
    
    
}

//要获取webView内容页的总高度，而因为可能网速,图片这些原因，他每次的contentsize是实时变化的，所以我们要监听webView的scrollView的contentSize的变化
- (void)addObserverForWebViewContentSize{
    
    [self.contentView.scrollView addObserver:self forKeyPath:@"contentSize" options:0 context:nil];
    
}

- (void)removeObserverForWebViewContentSize{
    
    [self.contentView.scrollView removeObserver:self forKeyPath:@"contentSize"];
    
}

// 监听结果回调事件
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

{
    
    //在这里边添加你的代码
    
    
    [self layoutCell];
    
}

//设置footerView的合理位置

- (void)layoutCell{
    
    //取消监听，因为这里会调整contentSize，避免无限递归
    
    [self removeObserverForWebViewContentSize];
    
//    UIView *viewss = [self.view viewWithTag:99999];
    
    CGSize contentSize =self.contentView.scrollView.contentSize;
    
//    UIView *vi = [[UIView alloc]init];
//    
//    vi.backgroundColor = [UIColor redColor];
//    
//    vi.userInteractionEnabled = YES;
//    
//    vi.tag = 99999;
//    
//    vi.frame = CGRectMake(0, contentSize.height, self.view.frame.size.width, 150);
    
    // 日期
    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _contentView.frame.size.height, self.view.frame.size.width - 20, self.view.frame.size.height / 20)];
    _dateLabel.font = [UIFont systemFontOfSize:15];
    _dateLabel.backgroundColor = [UIColor greenColor];
    _dateLabel.textColor = [UIColor grayColor];
    [_contentView.scrollView addSubview:_dateLabel];
    [_dateLabel release];
    //地址
    self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _dateLabel.frame.origin.y + _dateLabel.frame.size.height + 5 , self.view.frame.size.width - 20, _dateLabel.frame.size.height)];
    _addressLabel.font = [UIFont systemFontOfSize:15];
    _addressLabel.backgroundColor = [UIColor orangeColor];
    _addressLabel.textColor = [UIColor grayColor];
    [_contentView.scrollView addSubview:_addressLabel];
    [_addressLabel release];
    
    
    self.contentView.scrollView.contentSize = CGSizeMake(contentSize.width, contentSize.height + 150);
    
    //重新监听
    
//    [self addObserverForWebViewContentSize];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
